package com.c6906.vdam.monitor.controller;


import com.c6906.vdam.monitor.dto.GetindexsByTimesAck;
import com.c6906.vdam.monitor.dto.ResponseDTO;
import com.c6906.vdam.monitor.model.CpuUsage;
import com.c6906.vdam.monitor.model.ServerData;
import com.c6906.vdam.monitor.service.MonitorService;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.format.DateTimeParseException;
import java.util.List;

@RestController
@RequestMapping("/apis")
@Slf4j
public class MonitorController {

    @Autowired
    private MonitorService monitorService;


    @PostMapping("/getdata")
    public String handlePostRequest(@RequestBody JsonNode request) {
        log.info("Received POST request with data: {}", request);

        try {
            monitorService.saveData(request);
            return "Request processed successfully!";
        } catch (Exception e) {
            log.error("Error processing request", e);
            return "Error processing request";
        }
    }

    @GetMapping("/lastIndexes")
    public  ResponseDTO<List<ServerData>> getLastIndexes() {
        log.info("get  getLastIndexes");
        try {
            List<ServerData> latestRecords = monitorService.getLastIndex();
            return new ResponseDTO<>(200, "Success", latestRecords);
        } catch (Exception e) {
            log.error("Error processing request", e);
            return new ResponseDTO<>(400, "Failed");
        }
    }
    @GetMapping("/hosts")
    public  ResponseDTO<List<String>> hosts() {
        log.info("get  getLastIndexes");
        try {
            List<String> servers = monitorService.getAllServers();
            return new ResponseDTO<>(200, "Success", servers);
        } catch (Exception e) {
            log.error("Error processing request", e);
            return new ResponseDTO<>(400, "Failed");
        }
    }
    @GetMapping("/lastCpuUsage")
    public  ResponseDTO<List<CpuUsage>> lastCpuUsage() {
        log.info("get  lastCpuUsage");
        try {
            List<CpuUsage> lastCpuUsage = monitorService.lastCpuUsage();
            return new ResponseDTO<>(200, "Success", lastCpuUsage);
        } catch (Exception e) {
            log.error("Error processing request", e);
            return new ResponseDTO<>(400, "Failed");
        }
    }
    @GetMapping("/getindexsByTimes")
    public ResponseDTO<GetindexsByTimesAck> getindexsByTimes(
            @RequestParam("startTime") String startTime,
            @RequestParam("endTime") String endTime) {
        log.info("Received getindexsByTimes request with startTime: {} and endTime: {}", startTime, endTime);
        try {
            // 将字符串参数转换为 Timestamp
            Timestamp startTimestamp = Timestamp.valueOf(startTime.replace("T", " "));
            Timestamp endTimestamp = Timestamp.valueOf(endTime.replace("T", " "));
            // 调用服务层方法，传递转换后的 Timestamp 参数
            GetindexsByTimesAck ack = monitorService.getindexsByTimes(startTimestamp, endTimestamp);
            return new ResponseDTO<>(200, "Success", ack);
        } catch (DateTimeParseException e) {
            log.error("Invalid date format for startTime or endTime", e);
            return new ResponseDTO<>(400, "Invalid date format");
        } catch (Exception e) {
            log.error("Error processing request", e);
            return new ResponseDTO<>(400, "Failed");
        }
    }

}